<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/2 0002
 * Time: 17:44
 */

namespace Home\Controller;

use Think\Controller;

class TextController extends Controller
{
    public function _initialize()
    {
        if (!cookie('Yname') && cookie('Ypwd')) {
            $this->error('非法访问！！！请登录！！！', U('Index/index'), 3);
        }
    }

    public function show()

    {
        $typenumber = I('get.');
        if (cookie('Yname') && cookie('Ypwd')) {//查询是否是自动登录，是
            $map['name'] = cookie('Yname');
            $map['pwd'] = cookie('Ypwd');
            $result = M('admin')->where($map)->find();
            if (!$result == false) {//是否帐号密码存在并正确
                setcookie('Yname', cookie('Yname'), time() + 300, '/');
                setcookie('Ypwd', cookie('Ypwd'), time() + 300, '/');
                //查询信息
                $type = M('type')->select();
                $b = 0;
                $abc = array();
                foreach ($type as $k => $v) {
                    if ($k >= 3 && $k % 3 == 0) {
                        $b++;
                    }
                    $abc['t' . $b][] = $v;

                }
                $c = M('type')->count();
                $c = rand(1, $c);
                if ($typenumber) {
                    $goods = M('goods')->where('type=' . $typenumber['type'])->select();
                } else {
                    $goods = M('goods')->where('type=' . $c)->select();
                }
                foreach ($goods as $kk => $vv) {
                    $goods[$kk]['img'] = explode(',', $vv['img']);
                    $goods[$kk]['count'] = count($goods[$kk]['img']);
                }
//                $count = 0;
//                foreach ($goods as $nk => $nv) {
//                    if ($goods[$nk]['count'] > $count) {
//                        $count = $goods[$nk]['count'];
//                    }
//                }
//                $goods['count'] = $count;
                $this->assign('type', $abc);
                $this->assign('goods', $goods);
                $this->display();
            } else {//是否帐号密码存在并正确
                setcookie('Yname', null);
                setcookie('Ypwd', null);
                $this->error('账号或密码错误，请重新输入！', U('Index/index'), 3);
            }
        } else {//查询是否是自动登录，不是
            $form = I("post.");
            if ($form['name'] && $form['pwd'] || I("get.p")) {//是否输入账号和密码/翻页
                $result = M('admin')->where(array('name' => md5(md5($form['name']) . C('YAN')), 'pwd' => md5(md5($form['pwd']) . C('YAN'))))->find();
                if (!$result == false || I("get.p")) {//是否帐号密码存在并正确/翻页
                    if ($form['box']) {//是否勾选记住密码
                        setcookie('Yname', md5(md5($form['name']) . C('YAN')), time() + 3600 * 24, '/');
                        setcookie('Ypwd', md5(md5($form['pwd']) . C('YAN')), time() + 3600 * 24, '/');
                    } else {
                        setcookie('Yname', md5(md5($form['name']) . C('YAN')), time() + 300, '/');
                        setcookie('Ypwd', md5(md5($form['pwd']) . C('YAN')), time() + 300, '/');
                    }
                    //查询信息
                    $type = M('type')->select();
                    $b = 0;
                    $abc = array();
                    foreach ($type as $k => $v) {
                        if ($k >= 3 && $k % 3 == 0) {
                            $b++;
                        }
                        $abc['t' . $b][] = $v;
                    }
                    $c = M('type')->count();
                    $c = rand(1, $c);
                    if ($typenumber) {
                        $goods = M('goods')->where('type=' . $typenumber['type'])->select();
                    } else {
                        $goods = M('goods')->where('type=' . $c)->select();
                    }
                    foreach ($goods as $kk => $vv) {
                        $goods[$kk]['img'] = explode(',', $vv['img']);
                    }
                    $this->assign('type', $abc);
                    $this->assign('goods', $goods);
                    $this->display();
                } else {//是否帐号密码存在并正确
                    $email = M('admin')->where(array('Yname' => md5(md5($form['name']) . C('YAN'))))->find();
                    if ($email == false) {
                        $this->error('账号错误或不存在！，请重新输入！', U('Index/index'), 3);
                    }
                    $this->error('密码错误，请重新输入！', U('Index/index'), 3);
                }
            } else {//是否输入账号和密码
                $this->error('账号信息不全,请输入完整再登录！', U('Index/index'), 3);
            }
        }
    }

    public function upload()
    {
        $type = M('type')->select();
        $this->assign('type', $type);
        $this->display();
    }

    public function uploads()
    {


        $all = I('post.');
        foreach ($all['attribute'] as $k => $v) {
            $new['name'][] = $v;
            $new['price'][] = $all['price'][$k];
            $new['number'][] = $all['number'][$k];
            $new['unit'][] = $all['unit'][$k];
        }
        $json = json_encode($new);
        $img = '';
        $info['code'] = 0;
        foreach ($_FILES["file"]["error"] as $k => $v) {
            if ($_FILES["file"]["error"][$k]) {
                $info['info'] = '出错了!!!请检查文件类型是否为jpg/png或文件单个大小不超2M或全部之和大小不超8M!!!';
                $info['code'] = 1;
            } else {
                //没有出错
                //加限制条件
                //判断上传文件类型为png或jpg且大小不超过2M=2097152B
                if (($_FILES["file"]["type"][$k] == "image/png" || $_FILES["file"]["type"][$k] == "image/jpeg") && $_FILES["file"]["size"][$k] < 2097152) {
                    //防止文件名重复
                    $file = './Public/img/' . date("Y-m-d", time());

                    //转码，把utf-8转成gb2312,返回转换后的字符串， 或者在失败时返回 FALSE。
                    $file = iconv("UTF-8", "gb2312", $file);

                    //检查文件或目录是否存在
                    if (!file_exists($file)) {
                        mkdir($file, 0777, true);
                    }
                    $filename = $file . '/' . time() . mt_rand(1000, 9999) . strstr($_FILES["file"]["name"][$k], ".");
                    //保存文件,   move_uploaded_file 将上传的文件移动到新位置
                    move_uploaded_file($_FILES["file"]["tmp_name"][$k], $filename);//将临时地址移动到指定地址
                    $img .= $filename . ',';
                } else {
                    $info['info'] = '出错了!!!请检查文件类型是否为jpg/png或文件单个大小不超2M或全部之和大小不超8M!!!';
                    $info['code'] = 1;
                }
            }
        }
        $counts = count($_FILES['fa_img']['error']);
        for ($i = 0; $i < $counts; $i++) {
            $fa = "";
            foreach ($_FILES["fa_img"]["error"][$i] as $k => $v) {
                if ($_FILES["file"]["error"][$k]) {
                    $info['info'] = '出错了!!!请检查文件类型是否为jpg/png或文件单个大小不超2M或全部之和大小不超8M!!!';
                    $info['code'] = 1;
                } else {
                    //没有出错
                    //加限制条件
                    //判断上传文件类型为png或jpg且大小不超过2M=2097152B
                    if (($_FILES["fa_img"]["type"][$i][$k] == "image/png" || $_FILES["fa_img"]["type"][$i][$k] == "image/jpeg") && $_FILES["fa_img"]["size"][$i][$k] < 2097152) {
                        //防止文件名重复
                        $fa_img = './Public/img/' . date("Y-m-d", time());

                        //转码，把utf-8转成gb2312,返回转换后的字符串， 或者在失败时返回 FALSE。
                        $fa_img = iconv("UTF-8", "gb2312", $fa_img);

                        //检查文件或目录是否存在
                        if (!file_exists($fa_img)) {
                            mkdir($fa_img, 0777, true);
                        }
                        $fa_imgname = $fa_img . '/' . time() . mt_rand(1000, 9999) . strstr($_FILES["fa_img"]["name"][$i][$k], ".");
                        //保存文件,   move_uploaded_file 将上传的文件移动到新位置
                        move_uploaded_file($_FILES["fa_img"]["tmp_name"][$i][$k], $fa_imgname);//将临时地址移动到指定地址
                        $fa .= $fa_imgname . '++';
                    } else {
                        $info['info'] = '出错了!!!请检查文件类型是否为jpg/png或文件单个大小不超2M或全部之和大小不超8M!!!';
                        $info['code'] = 1;
                    }
                }
            }
            $fa_all[$i] =  rtrim($fa, "++");
        }
        $map['type'] = I('post.type');
        $map['content'] = $json;
        $map['img'] = rtrim($img, ",");
        $map['fa_img'] =json_encode($fa_all);
        $map['is_hot'] =I('post.c_hot')==1?1:0;

        $illustrate['title'] = I('post.title_m');
        $illustrate['num'] = I('post.num_m');
        $illustrate['price'] = I('post.price_m');
        $illustrate['count'] = count($fa_all);
        $map['illustrate'] = json_encode($illustrate);

        if ($info['code']) {
            $this->error($info['info'], U('upload'), 30);
        } else {
            if ($map['type'] && $map['content'] && $map['img']) {
                $re = M('goods')->add($map);
                if ($re === false) {
                    $this->error('上传失败！！！', U('upload'));
                } else {
                    $this->success('上传成功！！！', 'show');
                }
            } else {
                $this->error('上传失败！！！', U('upload'));
            }
        }
    }
}